package ch.unifr.diuf.webeng.taskmap.shared;

import ch.unifr.diuf.webeng.taskmap.client.Task;

/**
 * <p>
 * FieldVerifier validates that the name the user enters is valid.
 * </p>
 * <p>
 * This class is in the <code>shared</code> packing because we use it in both
 * the client code and on the server. On the client, we verify that the name is
 * valid before sending an RPC request so the user doesn't have to wait for a
 * network round trip to get feedback. On the server, we verify that the name is
 * correct to ensure that the input is correct regardless of where the RPC
 * originates.
 * </p>
 * <p>
 * When creating a class that is used on both the client and the server, be sure
 * that all code is translatable and does not use native JavaScript. Code that
 * is note translatable (such as code that interacts with a database or the file
 * system) cannot be compiled into client side JavaScript. Code that uses native
 * JavaScript (such as Widgets) cannot be run on the server.
 * </p>
 */
public class FieldVerifier {

	/**
	 * Verifies that the specified name is valid for our service.
	 * 
	 * @param name the name to validate
	 * @return  true if valid, false if invalid
	 */
	public static boolean isValidTitle(String name) {
		if (name == null) {
			return false;
		}
		return name.length() > 2;
	}
	
	
	/**
	 * Verifies that the specified address is valid for our service.
	 * 
	 * @param address  the address to validate
	 * @return  true if valid, false if invalid
	 */
	public static boolean isValidAddress(String address) {
		if (address == null) {
			return false;
		}
		return address.length() > 4;
	}
	
	/**
	 * Verifies that the specified hour is valid (between 00 and 24).
	 * 
	 * @param hour  the hour to validate
	 * @return  true if valid, false if invalid
	 */
	public static boolean isValidHour(String hour) {
		if (!hour.matches("\\d\\d") || hour == null || hour.length() < 1 || hour.length() > 2) {
			if(!hour.matches("\\d")){
				return false;
			}
		}
		int hourInt = Integer.decode(hour);
		if (hourInt < 0 || hourInt > 24) {
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * Verifies that the specified minute is valid (between 00 and 59).
	 * 
	 * @param minute  the minute to validate
	 * @return  true if valid, false if invalid
	 */
	public static boolean isValidMinute(String minute) {
		if (!minute.matches("\\d\\d") || minute == null || minute.length() < 1 || minute.length() > 2) {
			return false;
		}
		int minuteInt = Integer.decode(minute);
		if (minuteInt < 0 || minuteInt > 59) {
			return false;
		} else {
			return true;
		}
	}
	
	public static boolean isValidTime(String time) {
		String[] hm = time.split("\\:");
		if(!isValidHour(hm[0])){
			System.out.println("Hour "+hm[0]+" is not valid");
			return false;
		}
		if(!isValidMinute(hm[1])){
			System.out.println("Minute "+hm[1]+" is not valid");
			return false;
		}
		return true;
	}


	public static boolean isValidTask(Task task) {
		if(isValidTitle(task.getTitle()) && isValidAddress(task.getAddress()) && isValidTime(task.getStartTime()) && isValidTime(task.getEndTime())){
			return true;
		}
		return false;
	}
}
